<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    ES6 rest参数 - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						ES6 rest参数
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/08/16 01:38 上午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#ES6新特性</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<p><code>ES6</code>引入<code>rest</code>参数，用于获取函数的实参，用来代替<code>arguments</code></p>
<h2><a id="es5%E8%8E%B7%E5%8F%96%E5%AE%9E%E5%8F%82%E7%9A%84%E6%96%B9%E5%BC%8F" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>ES5获取实参的方式</h2>
<p>获取结果是对象[Object]</p>
<pre class="line-numbers"><code class="language-js">function date(){
    console.log(arguments);
}
date('卡夫','铃','四个八'); //输出对象['卡夫','铃','四个八']
</code></pre>
<h2><a id="es6-rest%E5%8F%82%E6%95%B0" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>ES6 rest参数</h2>
<p>获取结果是数组[Array]</p>
<p>可以使用<code>filter()</code>、<code>some()</code>、<code>every()</code>、<code>map()</code>等APi方法，提高参数处理的灵活程度</p>
<pre class="line-numbers"><code class="language-js">function date(...args){
    console.log(args);
}
date('卡夫','铃','四个八'); //输出数组['卡夫','铃','四个八']
</code></pre>
<p><strong><code>rest</code>参数必须放到参数最后</strong></p>
<pre class="line-numbers"><code class="language-js">function date(a, ...args){
    console.log(a);
    console.log(args);
}
date('卡夫','铃','四个八'); //a输出'卡夫',args输出数组['铃','四个八']
</code></pre>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>